﻿Imports System.Data.SqlClient
Imports System.Data
Public Class BookDetails1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if there is no Query String, if not redirects back to default page.
        If Request.Params("ID") = "" Then Response.Redirect("Default.aspx")
    End Sub

    Protected Function getViews(ByVal bookID As Integer) As String
        'Return the total views for this page.
        Dim result As String = ""
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("SELECT * FROM TotalViewsForAllBooks WHERE ID = @f1", conn)

        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", bookID)
        conn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dr.Read()
        result = dr("TotalViews")
        conn.Close()
        Return result
    End Function


    Protected Function getTotalComments() As String
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("SELECT * FROM TotalCommentsPerBook WHERE ID = @f1", conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dr.Read()
        Dim retValue As Integer = dr("TotalComments")
        If retValue <> 1 Then
            Return dr("TotalComments") & " Comments"
        Else
            Return dr("TotalComments") & " Comment"
        End If
        conn.Close()
        conn.Dispose()
    End Function

    Protected Sub btn_Validate_Click(sender As Object, e As EventArgs) Handles btn_Validate.Click
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("UPDATE Books SET IsVerified = @f1 WHERE ID = @f2", conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", 1)
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()
        'Eliminate cmd and conn from memory
        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Response.Redirect("~/Management/Books.aspx")
    End Sub

    Protected Function getRating(ByVal rating As Integer) As String
        Select Case (rating)
            Case Is = 1
                Return "Images/Rating/1-star-rating.png"
            Case Is = 2
                Return "Images/Rating/2-star-rating.png"
            Case Is = 3
                Return "Images/Rating/3-star-rating.png"
            Case Is = 4
                Return "Images/Rating/4-star-rating.png"
            Case Is = 5
                Return "Images/Rating/5-star-rating.png"
        End Select
        Return Nothing
    End Function

    
    Protected Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmdRating As New SqlCommand("DELETE FROM BookUserRating WHERE Book_FK = @f1", conn)
        cmdRating.CommandType = CommandType.Text
        cmdRating.Parameters.AddWithValue("@f1", Request.Params("ID"))
        conn.Open()
        cmdRating.ExecuteNonQuery()
        conn.Close()

        Dim cmd As New SqlCommand("DELETE FROM Books WHERE ID = @f1", conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()

        Response.Redirect("~/Management/Books.aspx")
    End Sub
End Class